function G(id)
{
    return document.getElementById(id);
}

function setIcon(iconName) { 
    var fullPath = iconName;
    try {
        chrome.browserAction.setIcon({path:fullPath});
    } catch(e) {
        console.error("Could not set browser action icon '" + fullPath + "'.");
    }
}

function updateUnreadCount(cnt) {
    unreadCount = "" + cnt;
    if (cnt > 0)
    { 
        chrome.browserAction.setBadgeText({text:unreadCount});
        chrome.browserAction.setBadgeBackgroundColor({color:[170, 80, 80, 255]});
    }
    else if (cnt < 0)
    {
        chrome.browserAction.setBadgeText({text:'x'});
        chrome.browserAction.setBadgeBackgroundColor({color:[170, 80, 80, 80]});
    }
    else
    {
        chrome.browserAction.setBadgeText({text:""});
    }
}

function check_status()
{
    var req = new XMLHttpRequest();
    var url = "http://t.baidu.com/notice/getCount?rn="+ 100 * Math.random();
    req.open("GET", url, true);
    req.onreadystatechange = function()
    {
        if (req.readyState == 4 && req.status == 200)
        {
            try{
            var responseText = req.responseText;
            eval('var response = ' + responseText + ';');
            var s = '';
            for (var i in response)
            {
                localStorage[i] = response[i];
            }
            var count_follow = Number(response.follow_count);
            var count_reply = Number(response.reply_count);
            var count_forward = Number(response.forward_count);
            var count_contact = Number(response.contact_count);
            var count_inbox = Number(response.inbox_count);
            var count_total = count_follow + count_reply + 
                count_forward + count_contact + count_inbox;
            if (response.user_id == 0)
            {
                updateUnreadCount(-1);
            }
            else if(count_total > 0)
            {
                updateUnreadCount(count_total);
            }
            else
            {
                updateUnreadCount(0);
            }

            }catch(e){}
        }
    };
    req.send(null);
}

function ISay() //wait
{
	if (G('content').value.length == 0)
	{
		G('msgstatus').innerHTML = "\u8bf4\u70b9\u5565\u5427\u540c\u5b66...";
		return;
	}
	if (G('content').value.length > 140)
	{
		G('msgstatus').innerHTML = "\u60a8\u592a\u7f57\u55e6\u4e86\uff0c\u8bf7\u4f7f\u7528\u97e9\u5cf0\u4f53\u3002";
		return;
	}
    var req = new XMLHttpRequest();
    var url = "http://t.baidu.com/message/post?" + Math.random();
    req.open("POST", url, true);
    req.setRequestHeader("Referer", "http://t.baidu.com");
    req.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=UTF-8");
    req.setRequestHeader("X-Request-By", "baidu.ajax");
    req.onreadystatechange = function()
    {
        if (req.readyState == 4)
        {
            var msg = G('msgstatus');
            if (req.status == 200)
            {
                eval('var r = ' + req.responseText + ';');
                var e = '';
                if (r.ret == undefined) {
                    e = '\u53d1\u9001\u6210\u529f';
                }else{
                    e ='\u53d1\u751f\u9519\u8bef';
                    if(r.ret == 2){
                        e = '\u8bf7\u5148\u767b\u5f55';
                    }else if(r.ret == 3){
                        e = '\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a';
                    }else if(r.ret == 5){
                        e = "\u60a8\u592a\u7f57\u55e6\u4e86\uff0c\u8bf7\u4f7f\u7528\u97e9\u5cf0\u4f53\u3002";
                    }else if(r.ret == 6){
                        e = '\u6d88\u606f\u5df2\u7ecf\u53d1\u51fa\uff0c\u65e0\u9700\u91cd\u590d\u63d0\u4ea4';
                    }else if(r.ret == 7){
                        e = '\u6d88\u606f\u539f\u6587\u5df2\u7ecf\u88ab\u5220\u9664,\u65e0\u6cd5\u8f6c\u53d1';
                    }else if(r.ret == 11){
                        e = '\u5185\u5bb9\u4e2d\u5305\u542b\u654f\u611f\u8bcd\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165';
					}
                }
                msg.innerHTML = e;
            }
            else
            {
                msg.innerHTML = 'Err...';
            }
            G('btnSend').disabled = '';
        }
    };
    var c = "m_content=" + encodeURIComponent(G('content').value)
        + "&pic_id=0&pic_filename=&pic_id_water=0&pic_filename_water=";
    G('btnSend').disabled = 'disabled';
    req.send(c);
}


// Looks through all open tabs for t.baidu.com
function tabSearch(tabs) {
    for(var i in tabs) {
        var tab = tabs[i];
        if(tab.url.indexOf(tqqurl) >= 0) {
            chrome.tabs.update(tab.id, {selected:true});
            return;
        }
    }
    chrome.tabs.create({url:tqqaturl});
}
